<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Customer
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Customer account controller
 *
 * @category   Mage
 * @package    Mage_Customer
 * @author      Magento Core Team <core@magentocommerce.com>
 */
require_once 'Mage/Customer/controllers/AccountController.php';
class PLUTUS_Home_Customer_AccountController extends Mage_Customer_AccountController
{
    /**
     * Create customer account action
     */
    public function indexAction()
    {
        $this->_redirect('*/*/edit');
        return;
    }

    public function createPostAction()
    {
        
        $session = $this->_getSession();
        if ($session->isLoggedIn()) {
            $this->_redirect('*/*/edit');
            return;
        }
        $session->setEscapeMessages(true); // prevent XSS injection in user input
        if ($this->getRequest()->isPost()) {
            $errors = array();

            if (!$customer = Mage::registry('current_customer')) {
                $customer = Mage::getModel('customer/customer')->setId(null);
            }
            /* Add new company name*/
            /*
            $customer = Mage::getModel('customer/customer')->
                    setCompany($this->getRequest()->getPost('company_name'));
            */
            /* @var $customerForm Mage_Customer_Model_Form */
            
            $customerForm = Mage::getModel('customer/form');
            $customerForm->setFormCode('customer_account_create')
                ->setEntity($customer);

            $customer->setCompanyInfo($this->getRequest()->getParam("company_info"));
            $customer->setAccountWebsite($this->getRequest()->getParam("account_website"));
            $customer->setFax($this->getRequest()->getParam("fax"));
            $customer->setCompany($this->getRequest()->getParam("company"));
            
            $customerData = $customerForm->extractData($this->getRequest());

            if ($this->getRequest()->getParam('is_subscribed', false)) {
                $customer->setIsSubscribed(1);
            }
            /**
             * Initialize customer group id
             */
            $customer->getGroupId();
            if ($this->getRequest()->getPost('create_address')) {
                /* @var $address Mage_Customer_Model_Address */
                $address = Mage::getModel('customer/address');
                /* @var $addressForm Mage_Customer_Model_Form */
                $addressForm = Mage::getModel('customer/form');
                $addressForm->setFormCode('customer_register_address')
                    ->setEntity($address);

                $addressData    = $addressForm->extractData($this->getRequest(), 'address', false);
                $addressErrors  = $addressForm->validateData($addressData);
                if ($addressErrors === true) {
                    $address->setId(null)
                        ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
                        ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
                    $addressForm->compactData($addressData);
                    
                    $customer->addAddress($address);

                    $addressErrors = $address->validate();
                    if (is_array($addressErrors)) {
                        $errors = array_merge($errors, $addressErrors);
                    }
                } else {
                    $errors = array_merge($errors, $addressErrors);
                }
            }
            if ($this->getRequest()->getPost('create_shipping_address')) {
                $shippingAddress = Mage::getModel('customer/address');

                $shippingAddressForm = Mage::getModel('customer/form');
                $shippingAddressForm->setFormCode('customer_register_address')
                    ->setEntity($shippingAddress);
                
                $shippingAddressData = array(
                    'firstname'  => $addressData['firstname'],
                    'lastname'   => $addressData['lastname'],
                    'company'    => $this->getRequest()->getPost('shipping_company'),
                    'street'     => $this->getRequest()->getPost('shipping_street'),
                    'city'       => $this->getRequest()->getPost('shipping_city'),
                    'country_id' => $this->getRequest()->getPost('shipping_country'),
                    'region'     => $this->getRequest()->getPost('shipping_region'),
                    'region_id'  => $this->getRequest()->getPost('shipping_region_id'),
                    'postcode'   => $this->getRequest()->getPost('shipping_postcode'),
                    'telephone'  => $this->getRequest()->getPost('telephone'),
                    'fax'        => $this->getRequest()->getPost('shipping_fax')
                    );

                $shippingAddressErrors = $addressForm->validateData($shippingAddressData);

                if ($shippingAddressErrors === true) {
                    $shippingAddress->setId(null)
                        ->setIsDefaultBilling($this->getRequest()->getParam('shipping_default_billing', false))
                        ->setIsDefaultShipping($this->getRequest()->getParam('shipping_default_shipping', false));

                    $shippingAddressForm->compactData($shippingAddressData);

                    $customer->addAddress($shippingAddress);

                    $shippingAddressErrors = $shippingAddress->validate();

                    if (is_array($shippingAddressErrors)) {
                        $errors = array_merge($errors, $shippingAddressErrors);
                    }
                } else {
                    $errors = array_merge($errors, $shippingAddressErrors);
                }
            }

            try {
                $customerErrors = $customerForm->validateData($customerData);
                if ($customerErrors !== true) {
                    $errors = array_merge($customerErrors, $errors);
                } else {
                    $customerForm->compactData($customerData);
                    $customer->setPassword($this->getRequest()->getPost('password'));
                    $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
                    $customerErrors = $customer->validate();
                    if (is_array($customerErrors)) {
                        $errors = array_merge($customerErrors, $errors);
                    }
                }

                $validationResult = count($errors) == 0;

                if (true === $validationResult) {
                    $customer->save();

                    Mage::dispatchEvent('customer_register_success',
                        array('account_controller' => $this, 'customer' => $customer)
                    );

                    if ($customer->isConfirmationRequired()) {
                        $customer->sendNewAccountEmail(
                            'confirmation',
                            $session->getBeforeAuthUrl(),
                            Mage::app()->getStore()->getId()
                        );
                        $session->addSuccess($this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())));
                        $this->_redirectSuccess(Mage::getUrl('*/*/edit', array('_secure'=>true)));
                        return;
                    } else {
                        $session->setCustomerAsLoggedIn($customer);
                        $url = $this->_welcomeCustomer($customer);
                        
                        $this->_redirectSuccess($url);
                        return;
                    }
                } else {
                    $session->setCustomerFormData($this->getRequest()->getPost());
                    if (is_array($errors)) {
                        foreach ($errors as $errorMessage) {
                            $session->addError($errorMessage);
                        }
                    } else {
                        $session->addError($this->__('Invalid customer data'));
                    }
                }
            } catch (Mage_Core_Exception $e) {
                $session->setCustomerFormData($this->getRequest()->getPost());
                if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                    $url = Mage::getUrl('customer/account/forgotpassword');
                    $message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
                    $session->setEscapeMessages(false);
                } else {
                    $message = $e->getMessage();
                }
                $session->addError($message);
            } catch (Exception $e) {
                $session->setCustomerFormData($this->getRequest()->getPost())
                    ->addException($e, $this->__('Cannot save the customer.'));
            }
        }

        $this->_redirectError(Mage::getUrl('*/*/create', array('_secure' => true)));
    }
     /**
     * Change customer password action
     */
    public function editPostAction()
    {
        
        if (!$this->_validateFormKey()) {
            return $this->_redirect('*/*/edit');
        }
        
        if ($this->getRequest()->isPost()) {
            /** @var $customer Mage_Customer_Model_Customer */
            $customer = $this->_getSession()->getCustomer();
            
            /** @var $customerForm Mage_Customer_Model_Form */
            $customerForm = Mage::getModel('customer/form');
            $customerForm->setFormCode('customer_account_edit')
                ->setEntity($customer);
            if ($this->getRequest()->getParam('is_subscribed', false)) {
                $customer->setIsSubscribed(1);
            }
            else{
                
                $customer->setIsSubscribed(0);
                //$customer->unIsSubscribed();
            }
            $customer->setCompanyInfo($this->getRequest()->getParam("company_info"));
            $customer->setAccountWebsite($this->getRequest()->getParam("account_website"));
            $customer->setFax($this->getRequest()->getParam("fax"));
            $customer->setCompany($this->getRequest()->getParam("company"));
            
            $customerData = $customerForm->extractData($this->getRequest());

            $errors = array();
            $customerErrors = $customerForm->validateData($customerData);
            
            /*********** Addres ****************************/
           

            /* @var $address Mage_Customer_Model_Address */
            $address  = Mage::getModel('customer/address');
            
            $addressId = $this->getRequest()->getParam('id');
            if ($addressId) {
                $existsAddress = $customer->getAddressById($addressId);
                if ($existsAddress->getId() && $existsAddress->getCustomerId() == $customer->getId()) {
                    $address->setId($existsAddress->getId());
                }
            }
            
            $errors = array();

            /* @var $addressForm Mage_Customer_Model_Form */
            $addressForm = Mage::getModel('customer/form');
            $addressForm->setFormCode('customer_address_edit')
                ->setEntity($address);
            $addressData    = $addressForm->extractData($this->getRequest());
            
            $addressErrors  = $addressForm->validateData($addressData);
            if ($addressErrors !== true) {
                $errors = $addressErrors;
            }

            try {
                $addressForm->compactData($addressData);
                $address->setCustomerId($customer->getId())
                    ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', true));
                    
                $addressErrors = $address->validate();
                if ($addressErrors !== true) {
                    $errors = array_merge($errors, $addressErrors);
                }

                if (count($errors) === 0) {
                    $address->save();
                    /*$this->_getSession()->addSuccess($this->__('The address has been saved.'));*/
                    
                } else {
                    $this->_getSession()->setAddressFormData($this->getRequest()->getPost());
                    foreach ($errors as $errorMessage) {
                        $this->_getSession()->addError($errorMessage);
                    }
                }
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
                    ->addException($e, $e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
                    ->addException($e, $this->__('Cannot save address.'));
            }
             $address  = Mage::getModel('customer/address');
            
            $addressId = $this->getRequest()->getParam('id');
            if ($addressId) {
                $existsAddress = $customer->getAddressById($addressId);
                if ($existsAddress->getId() && $existsAddress->getCustomerId() == $customer->getId()) {
                    $address->setId($existsAddress->getId());
                }
            }
            /****************Shipping Address************************/
            
            $errors = array();
            /* @var $address Mage_Customer_Model_Address */
            $addressShipping  = Mage::getModel('customer/address');
            
            $addressId = $this->getRequest()->getParam('shipping_id');
            if ($addressId) {
                $existsAddress = $customer->getAddressById($addressId);
                if ($existsAddress->getId() && $existsAddress->getCustomerId() == $customer->getId()) {
                    $addressShipping->setId($existsAddress->getId());
                }
            }
            /* @var $addressForm Mage_Customer_Model_Form */
            
            $addressShippingForm = Mage::getModel('customer/form');
            $addressShippingForm->setFormCode('customer_address_edit')
                ->setEntity($addressShipping);
            $addressShippingData = array(
                'firstname'  => $this->getRequest()->getPost('firstname'),
                'lastname'   => $this->getRequest()->getPost('lastname'),
                'company'    => $this->getRequest()->getPost('shipping_company'),
                'street'     => $this->getRequest()->getPost('shipping_street'),
                'city'       => $this->getRequest()->getPost('shipping_city'),
                'country_id' => $this->getRequest()->getPost('shipping_country'),
                'region'     => $this->getRequest()->getPost('shipping_region'),
                'region_id'  => $this->getRequest()->getPost('shipping_region_id'),
                'postcode'   => $this->getRequest()->getPost('shipping_postcode'),
                'telephone'  => $this->getRequest()->getPost('telephone'),
                'fax'        => $this->getRequest()->getPost('shipping_fax')
                );
            
            //$addressData    = $addressForm->extractData($this->getRequest());
            
            $addressShippingErrors  = $addressShippingForm->validateData($addressShippingData);
            if ($addressShippingErrors !== true) {
                $errors = $addressShippingErrors;
            }

            try {
                $addressShippingForm->compactData($addressShippingData);
                $addressShipping->setCustomerId($customer->getId())
                    ->setIsDefaultShipping(true);
                    
                $addressShippingErrors = $addressShipping->validate();
                if ($addressShippingErrors !== true) {
                    $errors = array_merge($errors, $addressErrors);
                }

                if (count($errors) === 0) {
                    $addressShipping->save();
                    /*$this->_getSession()->addSuccess($this->__('The address has been saved.'));*/
                    
                } else {
                    $this->_getSession()->setAddressFormData($this->getRequest()->getPost());
                    foreach ($errors as $errorMessage) {
                        $this->_getSession()->addError($errorMessage);
                    }
                }
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
                    ->addException($e, $e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
                    ->addException($e, $this->__('Cannot save address.'));
            }
            
            /****************shipping address */
            /*
            $shippingAddress = Mage::getModel('customer/address');

            $shippingAddressForm = Mage::getModel('customer/form');
            $shippingAddressForm->setFormCode('customer_register_address')
                ->setEntity($shippingAddress);

            $shippingAddressData = array(
                'firstname'  => $addressData['firstname'],
                'lastname'   => $addressData['lastname'],
                'company'    => $this->getRequest()->getPost('shipping_company'),
                'street'     => $this->getRequest()->getPost('shipping_street'),
                'city'       => $this->getRequest()->getPost('shipping_city'),
                'country_id' => $this->getRequest()->getPost('shipping_country'),
                'region'     => $this->getRequest()->getPost('shipping_region'),
                'region_id'  => $this->getRequest()->getPost('shipping_region_id'),
                'postcode'   => $this->getRequest()->getPost('shipping_postcode'),
                'telephone'  => $this->getRequest()->getPost('telephone'),
                'fax'        => $this->getRequest()->getPost('shipping_fax')
                );

            $shippingAddressErrors = $addressForm->validateData($shippingAddressData);

            if ($shippingAddressErrors === true) {
                $shippingAddress->setId(null)
                    ->setIsDefaultBilling($this->getRequest()->getParam('shipping_default_billing', false))
                    ->setIsDefaultShipping($this->getRequest()->getParam('shipping_default_shipping', false));

                $shippingAddressForm->compactData($shippingAddressData);

                $customer->addAddress($shippingAddress);

                $shippingAddressErrors = $shippingAddress->validate();

                if (is_array($shippingAddressErrors)) {
                    $errors = array_merge($errors, $shippingAddressErrors);
                }
            } else {
                $errors = array_merge($errors, $shippingAddressErrors);
            }
            */
            /*********** Addres ****************************/
            if ($customerErrors !== true) {
                $errors = array_merge($customerErrors, $errors);
            } else {
                $customerForm->compactData($customerData);
                $errors = array();

                // If password change was requested then add it to common validation scheme
                if ($this->getRequest()->getParam('change_password')) {
                    $currPass   = $this->getRequest()->getPost('current_password');
                    $newPass    = $this->getRequest()->getPost('password');
                    $confPass   = $this->getRequest()->getPost('confirmation');

                    $oldPass = $this->_getSession()->getCustomer()->getPasswordHash();
                    if (Mage::helper('core/string')->strpos($oldPass, ':')) {
                        list($_salt, $salt) = explode(':', $oldPass);
                    } else {
                        $salt = false;
                    }

                    if ($customer->hashPassword($currPass, $salt) == $oldPass) {
                        if (strlen($newPass)) {
                            /**
                             * Set entered password and its confirmation - they
                             * will be validated later to match each other and be of right length
                             */
                            $customer->setPassword($newPass);
                            $customer->setConfirmation($confPass);
                        } else {
                            $errors[] = $this->__('New password field cannot be empty.');
                        }
                    } else {
                        $errors[] = $this->__('Invalid current password');
                    }
                }

                // Validate account and compose list of errors if any
                $customerErrors = $customer->validate();
                if (is_array($customerErrors)) {
                    $errors = array_merge($errors, $customerErrors);
                }
            }

            if (!empty($errors)) {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
                foreach ($errors as $message) {
                    $this->_getSession()->addError($message);
                }
                $this->_redirect('*/*/edit');
                return $this;
            }

            try {
                $customer->setConfirmation(null);
                $customer->save();
                $this->_getSession()->setCustomer($customer)
                    ->addSuccess($this->__('The account information has been saved.'));

                $this->_redirect('*/*/edit');
                return;
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
                    ->addError($e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
                    ->addException($e, $this->__('Cannot save the customer.'));
            }
        }

        $this->_redirect('*/*/edit');
    }
}
